<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>出院登记</title>
</head>
<body>
<style type="text/css">
label{
	cursor: pointer;
}
.cost-span{
	display: inline-block;
    border: 1px solid #bbb;
    padding: 4px 10px;
    border-radius: 5px;
    color: red;
    font-size: 20px;
    min-width: 100px;
}
#outTabs .text-css{
	border: 0px;
    border-bottom: 1px solid #aaa;
    background-color: rgba(0,0,0,0);
    text-align: center;
    width: 80px;
    height: 24px;
    outline: none;
    font-style: italic;
    color:black;
}
.outPrintWin .text-css{
    background: bottom;
    border: 0;
    border-bottom: 1px solid black;
    text-align: center;
}
</style>
<script type="text/javascript">
$(function(){
	//出院登记的搜索框
	$("#outSear").searchbox({
		searcher:function(value){
			if($.trim(value)==""){
				return;
			}
			$.post(
				'ljw-leave-patient',
				{"hid":value},
				function(data){
					console.log(data);
					$("#outTabs input[name='nameText']").val(data[0].patient_name);
					$("#outTabs input[name='bedText']").val(data[0].bed_number);
					$("#outTabs input[name='sexText']").val(data[0].patient_sex);
					$("#outTabs input[name='ageText']").val(data[0].patient_age);
					$("#outTabs input[name='ksText']").val(data[0].departments_name);
					$("#outTabs input[name='doctorText']").val(data[0].user_name);
					$("#outTabs input[name='cidText']").val(data[0].card_cid);
					$("#outTabs input[name='phoneText']").val(data[0].mzcard_phone);
					$("#outTabs input[name='addressText']").val(data[0].home_address);
				}
			);
		}
	
	});
	
	//出院记录的表格初始化
	$("#outTable").datagrid({
		fitColumns:true,
		pagination:true,
	    pageSize:5,
	    pageList:[5,10],
		url:"ljw-leave",
		columns:[[
			{field:'hospital_id',title:"住 院 号",width:100,halign:'center'},
			{field:'patient_name',title:"姓 名",width:100,halign:'center'},
			{field:'patient_sex',title:'性 别',width:80,halign:'center'},
			{field:'bed_number',title:'床 位',width:50,halign:'center'},
			{field:'leave_time',title:'出 院 时 间',width:150,align:'center',
				formatter:function(value,row,index){
					var time = new Date(value).toJSON();
					var date = new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'') ;
					return date;
				}
			},
			{field:'leave_type',title:'出 院 类 型',width:80,align:'center'},
			{field:'departments_name',title:'科 室',width:100,halign:'center',},
			{field:'user_name',title:'主 治 医 生',width:100,halign:'center'},
			{field:'leave_name',title:'登 记 人',width:100,halign:'center'}
		]]
	});
	
	//出院记录的搜索框
	$("#outSear2").searchbox({
		searcher:function(value){
			$("#outTable").datagrid({
				fitColumns:true,
				pagination:true,
			    pageSize:5,
			    pageList:[5,10],
				url:"ljw-leave?hid="+value,
			});
		}
	});
	
	//确认登记按钮
	$("#outBtn").click(function(){
		var hid = $("#outSear").searchbox("getValue");
		var type = $("#outType").combobox("getValue");
		if(hid==""||hid==null)return;
		$.post(
			'ljw-leave-add',
			{"leaveType":type ,"hospital.hospitalId": hid },
			function(data){
				$("#outTable").datagrid("reload");
				$("#outTabs input[name='nameText']").val("");
				$("#outTabs input[name='bedText']").val("");
				$("#outTabs input[name='sexText']").val("");
				$("#outTabs input[name='ageText']").val("");
				$("#outTabs input[name='ksText']").val("");
				$("#outTabs input[name='doctorText']").val("");
				$("#outTabs input[name='cidText']").val("");
				$("#outTabs input[name='phoneText']").val("");
				$("#outTabs input[name='addressText']").val("");
			}
		);
	});
	
	//结算选择患者的下拉表格
	$('#costSear').combogrid({    
	    panelWidth:360,
	    idField: 'hospital_id',  
        textField:'patient_name', 
	    url:'ljw-leave-find',    
	    columns:[[    
	        {field:'hospital_id',title:'住 院 号',width:60,align:'center'}, 
	        {field:'patient_name',title:'姓 名',width:80,align:'center'},    
	        {field:'patient_sex',title:'性 别',width:50,align:'center'},    
	        {field:'card_cid',title:'身 份 证 号',width:160,align:'center'}  
	    ]],
	    onHidePanel:function( ){
	    	var v = $('#costSear').combogrid("getValue");
	    	$("input[name='cost-item']:eq(0)").prop('checked',"checked")
			initCost(v,1);
	    }
	});  
	
	$(".outTabs").tabs({//选项卡切换时就刷新
		onSelect:function(title){
			if(title=="费用结算"){
				$('#costSear').combogrid({
					url:'ljw-leave-find'
				});				
			}
		}
	});
	
	//结算按钮
	$("#clearOutBtn").click(function(){
		if ($(this).linkbutton('options').disabled == true) {
			return ;
		}
		var money = $("#cashBalance").text();
		var hid = $("#costSear").combogrid("getValue");
		$("#printBtn").linkbutton('enable');
		$.post(
			"ljw-cash-pay",
			{"i":2,"cashMoney":money,"hospital.hospitalId":hid},
			function (data){
				if(data){
					$.messager.show({
						title:'提 示 消 息',
						msg:'结 算 成 功 !',
						height:200,
						timeout:800,
						showType:'slide'
					});
					$('#clearOutBtn').linkbutton('disable');
					$("#printBtn").linkbutton('enable');
					$('#costSear').combogrid({
						url:'ljw-leave-find'
					});    
				}else{
					alert("结算失败");
				}
			}
		); 
	});
	
	//打印单据按钮
	$("#printBtn").click(function(){
		if ($(this).linkbutton('options').disabled == true) {
			return ;
		}
		$(".outPrintWin").window("open");
		var hid = $('#hidText-cost').textbox("getValue");
		$("#outPrintTable").datagrid({
			url:"ljw-cost?hid="+hid,
			fitColumns:true, 
			columns:[[
				{field:'hospital_id',hidden:true}, 
				{field:'patient_name',hidden:true},
				{field:'patient_age',hidden:true},
				{field:'patient_sex',hidden:true},
				{field:'Clearing_details_id',title:'编 号',width:60,halign:'center'},
				{field:'clearing_item',title:'费 用 名 称',width:200,halign:'center'},
				{field:'clearing_type',title:'类 型',width:100,align:'center'},
				{field:'Clearing_details_number',title:'数 量',width:50,halign:'center'},
				{field:'clearing_unit',title:'单 位',width:80,halign:'center'},
				{field:'Clearing_details_price',title:'单 价',width:100,halign:'center',
					formatter:function(value,row,index){
						return "￥" + value.toFixed(2);
					}	
				},
				{field:'total_price',title:'总 价',width:100,halign:'center',
					formatter:function(value,row,index){
						return "￥" + (row.Clearing_details_number*row.Clearing_details_price).toFixed(2);
					}	
				}
			]],
			onLoadSuccess:function(data){//加载成功后 
				console.log(data);
				var rows = $("#outPrintTable").datagrid("getRows");
				$(".numberOut").val(rows[0].hospital_id);
				$(".nameOut").val(rows[0].patient_name);
				$(".sexOut").val(rows[0].patient_sex);
				$(".ageOut").val(rows[0].patient_age);
				$(".cashOut").val($("#cashTotal").text());
				$(".costOut").val($("#costTotal").text());
				$(".returnOut").val($("#cashBalance").text());
			}
		});
		$("#printBtn").linkbutton('disable');
	});
	
	//打印完成
	$("#outPrintOkBtn").click(function(){
		$(".outPrintWin").window("close");
		$("#costTotal").text("");
		$("#cashTotal").text("");
		$("#cashBalance").text("");
		$("#costOutTable").datagrid("reload");
	});
	
	//费用类别
	$("input[name='cost-item']").click(function(){
		var v = $('#costSear').combogrid("getValue");
		if(v==""||v==null){
			return ;
		}
		$.post(
			"ljw-clearDetails?hid="+ v +"&type="+$(this).val(),
			function(data){ 
				$("#costOutTable").datagrid("loadData", data);
				console.log(data);
			}
		);
	});
	
	//费用明细表格
	$("#costOutTable").datagrid({
		columns:[[
			{field:'total',hidden:true},
			{field:'patient_name',hidden:true},
			{field:'Clearing_details_id',title:'编 号',width:60,halign:'center'},
			{field:'clearing_item',title:'费 用 名 称',width:200,halign:'center'},
			{field:'clearing_type',title:'类 型',width:100,align:'center'},
			{field:'Clearing_details_number',title:'数 量',width:50,halign:'center'},
			{field:'clearing_unit',title:'单 位',width:80,halign:'center'},
			{field:'Clearing_details_price',title:'单 价',width:100,halign:'center',
				formatter:function(value,row,index){
					return "￥" + value.toFixed(2);
				}	
			},
			{field:'total_price',title:'总 价',width:100,halign:'center',
				formatter:function(value,row,index){
					return "￥" + (row.Clearing_details_number*row.Clearing_details_price).toFixed(2);
				}	
			},
			{field:'tally_time',title:'记 账 时 间',width:200,align:'center',
				formatter:function(value,row,index){
					var time = new Date(value).toJSON();
					var date = new Date(+new Date(time)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'') ;
					return date;
				},	
			}
		]]
	});
	
});

//费用明细表格初始化数据
function initCost(hid){
	$("#costOutTable").datagrid({
		url:"ljw-cost?hid="+hid+"&opin="+1,
		fitColumns:true, 
	    onLoadSuccess:function(data){//加载成功后 
	    	var rows = $("#costOutTable").datagrid("getRows");
	    	if($("input[name='cost-item']:checked").val()==3){
	    		if(rows.length==0){
		    		$("#costTotal").text("");
					$("#cashTotal").text("");
					$("#cashBalance").text("");
					$("#hidText-cost").textbox("setValue","");
					$("#clearOutBtn").linkbutton("disable");
			    	console.log("--"+rows.length);
		    		return;
		    	}
	    	}
	    	if(rows.length==0){
	    		return;
	    	}
	    	if(!rows[0].hasOwnProperty('totalCash')){
		    	console.log("-3333-");
	    		return;
	    	} 
	    	console.log("-355553-");
			var costTotal = parseInt(rows[0].totalCash);//费用合计
			var cashTotal = parseInt(rows[0].total);//累计押金
			var cashBalance = cashTotal - costTotal;//押金余额
			$('#clearOutBtn').linkbutton('enable');
			$("#costTotal").text(costTotal.toFixed(2));
			$("#cashTotal").text(cashTotal.toFixed(2));
			$("#cashBalance").text(cashBalance.toFixed(2));
			$("#hidText-cost").textbox("setValue",rows[0].hospital_id);
	    }
	});
}
</script>
<div class="easyui-tabs outTabs"  style="width: 100%;height: 100%;">
	<div title='出院登记' id="outTabs" style="padding: 5px 70px;">
	    <fieldset style="border-radius: 5px;width:1000px;border: 2px solid darkseagreen;">
	    	<legend>个人信息</legend>
			<p style="margin-top: 5px;">
				<input id="outSear" class="easyui-searchbox" data-options="label:'住院号：',labelAlign:'right'" style="width:200px;height:28px;padding-left:5px;">
				<span style="display: inline-block;margin-left: 40px;">
					床位：<input name="bedText" class="text-css" style="width: 50px" disabled="disabled">
				</span>
				<span style="display: inline-block;margin-left: 40px;">
					姓名：<input name="nameText" class="text-css" disabled="disabled" style="width: 120px;">
				</span>
				<span style="display: inline-block;margin-left: 40px;">
					性别：<input name="sexText" class="text-css" style="width: 50px" disabled="disabled">
				</span>
				<span style="display: inline-block;margin-left: 40px;">
					年龄：<input name="ageText" class="text-css" style="width: 50px" disabled="disabled">
				</span>
			</p>
			<p>
				<span style="display: inline-block;margin-left: 25px;">
					联系电话：<input name="phoneText" class="text-css" disabled="disabled" style="width: 85px;padding: 0 15px;">
				</span>
				<span style="display: inline-block;margin-left: 40px;">
					身份证号：<input name="cidText" class="text-css" disabled="disabled" style="width: 135px;padding: 0 15px;">
				</span>
				<span style="display: inline-block;margin-left: 40px;">
					家庭住址：<input name="addressText" class="text-css" disabled="disabled" style="width: 360px;">
				</span>
			</p>
			<p style="margin-bottom: 5px;">
				<span style="display: inline-block;margin-left: 25px;">
					科室：<input name="ksText" class="text-css" style="width: 120px" disabled="disabled">
				</span>
				<span style="display: inline-block;margin-left: 40px;margin-right: 30px;">
					主治医师：<input name="doctorText" class="text-css" style="width: 100px" disabled="disabled">
				</span>
				<select id="outType" name="departments_id" class="easyui-combobox" panelHeight="auto"
				 	data-options="label:'出院类型：',labelAlign:'right'" style="width:220px;cursor: pointer;">
					<option value="康复">康复</option>
					<option value="个人原因">个人原因</option>
				</select>
				<a id="outBtn" class="easyui-linkbutton" iconCls="icon-save" style="padding:0 5px;margin-left:65px ">确认登记</a>
			</p>
	    </fieldset>
	    <fieldset style="border-radius: 5px;width:1000px;margin-top: 9px;border: 2px solid darkseagreen;">
	    	<legend>出院记录</legend>
			<div style="padding-bottom: 5px;">
				<input id="outSear2" class="easyui-searchbox" data-options="label:'住院号：',labelAlign:'right'" style="width:200px;height:28px;padding-left:5px;">
			</div>
			<table id="outTable" class="easyui-datagrid" style="height:220px ">
				
			</table>
		</fieldset>
		
		<!-- 打印弹框  Start -->
		<div class="easyui-window outPrintWin" title="打 印 单 据" style="width:680px;user-select:none;"
			data-options="modal:true,collapsible:false,minimizable:false,
			maximizable:false,closed:true,draggable:false,shadow:true,resizable:false" style="padding:20px 40px">
			<div style="text-align: center;font-size: 22px;margin: 5px;">
				平 安 医 院
			</div>
			<div style="text-align: center;font-size: 16px;padding: 6px;border-bottom: 1px solid #CCC;">
				住院费用结算收据
			</div>
			<p>
				<span style="display: inline-block;margin-left: 30px;">
					住院号：<input class="text-css numberOut" disabled="disabled" style="width: 90px;">
				</span>
				<span style="display: inline-block;margin-left: 30px;">
					姓名：<input class="text-css nameOut" disabled="disabled" style="width: 110px;">
				</span>
				<span style="display: inline-block;margin-left: 30px;">
					性别：<input class="text-css sexOut" style="width: 55px" disabled="disabled">
				</span>
				<span style="display: inline-block;margin-left: 30px;">
					年龄：<input class="text-css ageOut" style="width: 55px" disabled="disabled">
				</span>
			</p>
			<p>
				<span style="display: inline-block;margin-left: 30px;">
					总押金：<input class="text-css cashOut" disabled="disabled" style="width: 110px;color: red;">
				</span>
				<span style="display: inline-block;margin-left: 30px;">
					总费用：<input class="text-css costOut" disabled="disabled" style="width: 110px;color: red;">
				</span>
				<span style="display: inline-block;margin-left: 30px;">
					退还押金：<input class="text-css returnOut" style="width: 110px;color: red;" disabled="disabled">
				</span>
			</p>
			<p>
				 
			</p>
			<table id="outPrintTable" class="easyui-datagrid" style="height:280px" data-options="fitColumns:true">
			     
			</table> 
			<div style="background: #dee;padding: 10px;text-align: center;">
				<a id="outPrintOkBtn" iconCls="icon-ok2" class="easyui-linkbutton" style="padding:0 5px;" id="opsBtn-ok">完成</a>
			</div>
		</div>
		<!-- 打印弹框  End-->
		
	</div>
	
	<div title='费用结算' >
		<fieldset style="width: 1100px;overflow: auto;margin: 10px auto;border: 1px solid darkseagreen;border-radius: 5px;padding: 10px;">
			<legend>结算费用</legend>
			<div style="">
				<p style="margin: 5px 0;border: 1px solid #ddd;padding: 5px;margin-bottom: 8px;">
					<input id="costSear" class="easyui-combogrid" 
							data-options="label:'姓名：',labelAlign:'right'" style="width:200px;height:28px;padding-left:5px;">
					<input id="hidText-cost" class="easyui-textbox" 
							data-options="label:'住院号：',labelAlign:'right',editable:false" style="width:200px;height:28px;padding-left:5px;">
					<label style="display: inline-block;margin-left: 80px;">
						<input type="radio" value='3' name="cost-item" checked="checked">个人合计
					</label>
					<label style="display: inline-block;margin-left: 30px;">
						<input type="radio" value='2' name="cost-item">药品明细
					</label>
					<label style="display: inline-block;margin-left: 30px;">
						<input type="radio" value='1' name="cost-item">诊疗明细
					</label>
				</p>
			</div>
			<div style="float: left;">
				<table id="costOutTable" class="easyui-datagrid" fitColumns=true style="width:815px;height:300px">
					 
				</table>
			</div>
			<div style="border: 1px solid darkseagreen;height: 280px;float: right;min-width: 230px;padding: 20px 10px 0 30px;border-radius: 5px;">
				<p style="margin: 20px 0;">累计押金：￥<span id="cashTotal" class="cost-span"></span></p>
				<p style="margin: 30px 0;">费用合计：￥<span id="costTotal" class="cost-span"></span></p>
				<p style="margin: 30px 0;">押金余额：￥<span id="cashBalance" class="cost-span"></span></p>
				<p style="margin-top: 30px;">
					<a id="clearOutBtn" disabled=true class="easyui-linkbutton" style="margin-left: 5px;margin-top: 5px;padding: 0 8px;">确 定 结 算</a>
					<a id="printBtn" disabled=true class="easyui-linkbutton" style="margin-top: 5px;padding: 0 8px;">打 印 单 据</a>
				</p>
			</div>
		</fieldset>
	</div>
</div>
</body>
</html>